package com.hangwei.aicabinet.presenter;

import android.support.v4.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hangwei.aicabinet.manager.NetManager;
import com.hangwei.aicabinet.net.ResponseInfo;
import com.hangwei.aicabinet.net.ViewCallBack;
import com.hangwei.aicabinet.util.LogUtil;
import com.hangwei.aicabinet.util.ToastUtil;
import com.hangwei.aicabinet.view.BaseView;

import java.util.ArrayList;

import rx.Observable;

/**
 * author: 张龙臣
 * date: 2018-08-14.
 */

public abstract class RecyclerViewPresenter<T, V extends BaseView> extends BasePresenter {

    private static final int PAGE_SIZE = 10;
    protected final Object[] mParams;
    private int mNextRequestPage = 2; //第一条数据是在外面请求的
    private SwipeRefreshLayout mRefreshLayout;
    private BaseQuickAdapter mAdapter;

    public RecyclerViewPresenter(V baseView, SwipeRefreshLayout refreshLayout, BaseQuickAdapter adapter, Object... params) {
        super(baseView);
        mRefreshLayout = refreshLayout;
        mAdapter = adapter;
        mParams = params;
    }

    protected abstract Observable<ResponseInfo<ArrayList<T>>> getObservable(int page);

    public void loadMore() {
//        if (mNextRequestPage == 1) {
//            LogUtil.e("------------------loadMore-----------------wtf---------ERROR---------------------");
//        }
        LogUtil.i("-------------loadMore-----------mNextRequestPage = " + mNextRequestPage);
        mSubscription = NetManager.getInstance().doSubscribe(new NetManager.Model<ArrayList<T>>() {
            @Override
            protected Observable<ResponseInfo<ArrayList<T>>> getObservable() {
                return RecyclerViewPresenter.this.getObservable(mNextRequestPage);
            }

            @Override
            protected void onNext(ArrayList<T> data) {
                setData(false, data);
            }

            @Override
            protected void onError() {
                mAdapter.loadMoreFail();
            }
        });
    }


    public void refresh() {
        mNextRequestPage = 1;
        mAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
//        LogUtil.e("------------------refresh-----------------wtf------------mNextRequestPage = " + mNextRequestPage);
        mSubscription = NetManager.getInstance().doSubscribe(new NetManager.Model<ArrayList<T>>() {
            @Override
            protected Observable<ResponseInfo<ArrayList<T>>> getObservable() {
                return RecyclerViewPresenter.this.getObservable(mNextRequestPage);
            }

            @Override
            protected void onNext(ArrayList<T> data) {
                mAdapter.setEnableLoadMore(true);
                mRefreshLayout.setRefreshing(false);
                setData(true, data);
            }

            @Override
            protected void onError() {
                mAdapter.setEnableLoadMore(true);
                mRefreshLayout.setRefreshing(false);
            }
        });
    }

    public void refresh(final ViewCallBack callBack) {
        callBack.onNetworkStart();
        mNextRequestPage = 1;
//        LogUtil.e("------------------refresh----callBack-------wtf------------mNextRequestPage = " + mNextRequestPage);
        mAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        mSubscription = NetManager.getInstance().doSubscribe(new NetManager.Model<ArrayList<T>>() {
            @Override
            protected Observable<ResponseInfo<ArrayList<T>>> getObservable() {
                return RecyclerViewPresenter.this.getObservable(mNextRequestPage);
            }

            @Override
            protected void onNext(ArrayList<T> data) {
                mAdapter.setEnableLoadMore(true);
                mRefreshLayout.setRefreshing(false);
                setData(true, data);
                callBack.onNetworkComplete(true);
            }

            @Override
            protected void onError() {
                mAdapter.setEnableLoadMore(true);
                mRefreshLayout.setRefreshing(false);
                callBack.onNetworkComplete(false);
            }
        });
    }

    public void setData(boolean isRefresh, ArrayList<T> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (mNextRequestPage == 2) {
//                LogUtil.e("-----------------------------------wtf------------------------------");
                mAdapter.setNewData(data);
            } else if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局(  wtf  不显示且不足一屏就自动加载，page还不对，坑死劳资了 )
//            mAdapter.loadMoreEnd(!isRefresh);
            mAdapter.loadMoreEnd(false);
            if (mNextRequestPage == 1) {//不需要的提醒
                if (isRefresh) {
                    ToastUtil.showToast("数据更新成功");
                } else
                    ToastUtil.showToast("没有更多数据了");
            }
        } else {
            mAdapter.loadMoreComplete();
        }
//        LogUtil.i("AAAAAAAAAAAAA RecyclerViewPresenter ----------- mAdapter size = " + mAdapter.getData().size() + "\n" + "  isRefresh  " + isRefresh);
    }

}
